# 1、练习封装案例（和上课保持一致即可）
#1，大象，大象初始体重。跑一次减0.5kg，吃一次增加1kg
class Animal:
    def __init__(self,name,weight):
        self.name =name
        self.weight =weight

    def eat(self):
        print(f'{self.name}吃了一次食物，体重增加了1kg,现在的体重是{self.weight+1}')

    def run(self):
        print(f'{self.name}跑了一次，体重减少了0.5kg，现在的体重是{self.weight-0.5}kg')

    def __str__(self):
        return f'{self.name}宝宝真辛苦'



#2，士兵突击
class Gun():
    def __init__(self,model):
        self.model = model
        self.bullet_count=0

    def add_bull(self,count):
        self.bullet_count+=count

    def shoot(self):
        if self.bullet_count>=1:
            print(f"还有子弹，可以射击")
        else:
            print('没有子弹了')

            # return

        self.bullet_count-=1
        print('%s 射击后还剩 %s枚子弹' % (self.model,self.bullet_count))

class Soldier:
    def __init__(self,name,gun:Gun=None ):
        self.name = name
        self.gun =gun

    def fire(self):
        if self.gun is None:
            print('[%s] 请去捡把枪' % self.name)

            return
        print(f'{self.name}发起冲锋，，，')

        #装子弹
        # self.gun.add_bull(0)
        #射击
        self.gun.shoot()

#家具，房子
#先定义家具类，包括名字和占地面积。屋子剩余面积，家具类型
class HouseItem:
    def __init__(self,name,area):
        self.name = name
        self.area = area

    def __str__(self):
        return f'安装{self.name}，会占用{self.area}面积'


class House:
    def __init__(self,house_type,area):
        self.house_type = house_type
        self.area = area
        self.free_area = area
        self.items =[]

    def __str__(self):
        # return f'户型是，总面积有，剩余面积有，家具有'
        return ('户型是%s\n总面积有%.2f  剩余[%.2f]\n家具: %s'
                %(self.house_type,self.area,self.free_area,self.items))

    def add_item(self,item:HouseItem):
        if self.free_area >=item.area:
            self.items.append(item.name)
            self.free_area -= item.area
        else:
            print(f'房子太小了，装不下了')
            return





if __name__ == '__main__':
    Elephant = Animal('Elephant',80)
    Elephant.eat()
    Elephant.run()
    print(Elephant)
    Tiger = Animal('Tiger',60)
    Tiger.eat()
    Tiger.run()
    print(Tiger)
    print('*'*50)

    m4a1 =Gun('m4a1')
    m4a1.add_bull(30)
    lqq =Soldier('lqq')
    lqq.fire()
    lqq.gun=m4a1
    lqq.fire()
    print('*'*50)

    bed =HouseItem('席梦思',5)
    chese =HouseItem('躺椅',0.5)
    table =HouseItem('大圆桌',5)
    print(bed)
    house =House('三室一厅',10)
    house.add_item(bed)
    house.add_item(chese)
    house.add_item(table)
    print(house)
